package com.kehutong.house.admin;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.house.entity.House;
import com.kehutong.house.enums.*;
import com.kehutong.house.service.HouseService;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@MvcAction
@ReqMapping("/admin/basic/house")
public class HouseController {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private DoveClient doveClient;
    @Inject
    private HouseService houseService;

    @HasPermission("basic:house:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
        return houseService.page(jsonObject);
    }

    @HasPermission("basic:house:view")
    @ReqMapping("/get")
    public Object get(House house) throws Exception {
        return houseService.getHouses(house);
    }

    @Bean(newInstance = true, copy = true)
    @HasPermission("basic:house:edit")
    @ReqMapping("/save")
    public Object save(Token token, House house, JSONObject jsonObject) throws Exception {
        return houseService.insert(token, house, jsonObject);
    }

    @Bean(copy = true, isIgnoreNull = false)
    @HasPermission("basic:house:edit")
    @ReqMapping("/update")
    public Object update(Token token, House house, JSONObject jsonObject) throws Exception {
        return houseService.update(token, house, jsonObject);
    }

    @ReqMapping("/enums")
    public Object getEnums() {
        Map<String, Object> map = new HashMap<>();
        map.put("propertyTypes", Arrays.asList(PropertyType.values()));
        map.put("salesStatus", Arrays.asList(SalesStatus.values()));
        map.put("harvestStatus", Arrays.asList(HarvestStatus.values()));
        map.put("decoratedStasus", Arrays.asList(DecoratedStasus.values()));
        map.put("resideStasus", Arrays.asList(ResideStasus.values()));
        map.put("rentStasus", Arrays.asList(RentStasus.values()));
        return Result.success(map);
    }

    @HasPermission("basic:house:edit")
    @ReqMapping("/delete")
    public Object delete(House house) throws Exception {

        JSONArray array = new JSONArray();
        JSONObject json = new JSONObject();
        json.put("id", house.getOwnerNo());
        json.put("type", "1");
        json.put("houseNo", house.getId());
        array.add(json);
        if (Objects.nonEmpty(array)) {
            doveClient.post("/crm/resident/service/batchDeleteResidentHouse", (http) -> {
                JSONObject body = new JSONObject();
                body.put("array", array);
                http.setBody(body.toJSONString());
            });
        }

       return houseService.delete(house);
    }

    @HasPermission("basic:house:edit")
    @ReqMapping("/deleteAll")
    public Object deleteAll(JSONObject jsonObject) throws Exception {
        List<House> list = jdbcSession.findArray(House.class)
                .in("id", jsonObject.getJSONArray("ids"))
                .eq("deleted", false)
                .exe();

        JSONArray array = new JSONArray();
        list.forEach(item -> {
            JSONObject json = new JSONObject();
            json.put("id", item.getOwnerNo());
            json.put("type", "1");
            json.put("houseNo", item.getId());
            array.add(json);
        });

        if (Objects.nonEmpty(array)) {
            doveClient.post("/crm/resident/service/batchDeleteResidentHouse", (http) -> {
                JSONObject body = new JSONObject();
                body.put("array", array);
                http.setBody(body.toJSONString());
            });
        }

        list.forEach(item -> {
            item.setDeleted(true);
        });
        jdbcSession.updateById(list);

        return Result.success();
    }

    @HasPermission("basic:house:view")
    @ReqMapping("/getHouseTransfer")
    public Object getHouseTransfer(Token token, JSONObject jsonObject) throws Exception {
        return houseService.getHouseTransfer(token, jsonObject, true);
    }

    /**
     *
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     * @author zwp
     * @date 2021/07/06 09:41
     */
    @HasPermission("basic:house:view")
    @ReqMapping("/getHouseByLevel")
    public Object getHouseByLevel(Token token, JSONObject jsonObject) throws Exception {
        return houseService.getHouseTransfer(token, jsonObject, false);
    }
    /**
     *
     * @param jsonObject
     * @return
     * @throws Exception
     * @author zwp
     * @date 2021/07/06 10:10
     */
    @HasPermission("basic:house:view")
    @ReqMapping("/getUserListByHouseIds")
    public Object getUserListByHouseIds(Token token,JSONObject jsonObject) throws Exception {
        JSONArray residentHouseList =null;
        JSONObject residentHouseResult = doveClient.post("/crm/resident/service/getUserListByHouseIds", (http) -> {
            http.addHeader("companyNo", token.getCompanyNo());
            JSONObject body = new JSONObject();
            body.put("houseIds", jsonObject.getJSONArray("houseIds"));
            http.setBody(body.toJSONString());
        });
        if (residentHouseResult.getIntValue("code") == 200) {
            residentHouseList = residentHouseResult.getJSONArray("data");
        }
       return Result.success(residentHouseList);
    }


    @HasPermission("basic:house:view")
    @ReqMapping("/export")
    public Object export(JSONObject jsonObject) throws Exception {
        return houseService.export(jsonObject);
    }

    @ReqMapping("/checkFile")
    public Object checkFile(JSONObject jsonObject) throws Exception {
        return houseService.checkFile(jsonObject);
    }

    @ReqMapping("/readFile")
    public Object readFile(Token token, JSONObject jsonObject) throws Exception {
        return houseService.readFile(token, jsonObject);
    }

    @ReqMapping("/setStatus")
    public Object setStatus(House entity) {
        entity.setEnable(!entity.isEnable());
        jdbcSession.updateById(entity);

        return Result.success();
    }
}
